package org.rsbot.script.methods;

import java.awt.Point;
import java.util.LinkedList;
import org.rsbot.client.Client;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSItemDef;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

public class Inventory extends MethodProvider
{
  public static final int INTERFACE_INVENTORY = 149;
  public static final int INTERFACE_INVENTORY_BANK = 763;
  public static final int INTERFACE_INVENTORY_SHOP = 621;

  Inventory(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public RSComponent getInterface()
  {
    RSComponent localRSComponent;
    if (this.methods.interfaces.get(763).isValid()) {
      localRSComponent = this.methods.interfaces.getComponent(763, 0);

      if ((localRSComponent != null) && (localRSComponent.getAbsoluteX() > 50)) {
        return localRSComponent;
      }
    }
    if (this.methods.interfaces.get(621).isValid()) {
      localRSComponent = this.methods.interfaces.getComponent(621, 0);

      if ((localRSComponent != null) && (localRSComponent.getAbsoluteX() > 50)) {
        return localRSComponent;
      }

    }

    if (this.methods.game.getCurrentTab() != 4) {
      this.methods.game.openTab(4);
      sleep(random(400, 900));
    }

    return this.methods.interfaces.getComponent(149, 0);
  }

  public void dropAllExcept(boolean paramBoolean, int[] paramArrayOfInt)
  {
    RSTile localRSTile = this.methods.players.getMyPlayer().getLocation();
    while ((getCountExcept(paramArrayOfInt) != 0) && 
      (this.methods.calc.distanceTo(localRSTile) <= 100))
    {
      int i;
      int j;
      int k;
      int m;
      if (!paramBoolean) {
        for (i = 0; i < 4; i++) {
          for (j = 0; j < 7; j++)
          {
            k = 0;
            for (m = 0; (m < paramArrayOfInt.length) && (k == 0); m++) {
              k = paramArrayOfInt[m] == getItems()[(i + j * 4)].getID() ? 1 : 0;
            }
            if (k == 0)
              dropItem(i, j);
          }
        }
      }
      else {
        for (i = 0; i < 7; i++) {
          for (j = 0; j < 4; j++) {
            k = 0;
            for (m = 0; (m < paramArrayOfInt.length) && (k == 0); m++) {
              k = paramArrayOfInt[m] == getItems()[(j + i * 4)].getID() ? 1 : 0;
            }
            if (k == 0) {
              dropItem(j, i);
            }
          }
        }
      }
      sleep(random(500, 800));
    }
  }

  public boolean dropAllExcept(int[] paramArrayOfInt)
  {
    dropAllExcept(false, paramArrayOfInt);
    return true;
  }

  public void dropItem(int paramInt1, int paramInt2)
  {
    if (this.methods.interfaces.canContinue()) {
      this.methods.interfaces.clickContinue();
      sleep(random(800, 1300));
    }
    if ((this.methods.game.getCurrentTab() != 4) && (!this.methods.interfaces.get(762).isValid()) && (!this.methods.interfaces.get(620).isValid()))
    {
      this.methods.game.openTab(4);
    }
    if ((paramInt1 < 0) || (paramInt1 > 3) || (paramInt2 < 0) || (paramInt2 > 6))
      return;
    if (getItems()[(paramInt1 + paramInt2 * 4)].getID() == -1) {
      return;
    }
    Point localPoint = this.methods.mouse.getLocation();
    if ((localPoint.x < 563 + paramInt1 * 42) || (localPoint.x >= 563 + paramInt1 * 42 + 32) || (localPoint.y < 213 + paramInt2 * 36) || (localPoint.y >= 213 + paramInt2 * 36 + 32))
    {
      this.methods.mouse.move(getInterface().getComponents()[(paramInt2 * 4 + paramInt1)].getCenter(), 10, 10);
    }

    this.methods.mouse.click(false);
    sleep(random(10, 25));
    this.methods.menu.doAction("drop");
    sleep(random(25, 50));
  }

  public boolean contains(int paramInt)
  {
    return getItem(new int[] { paramInt }) != null;
  }

  public boolean containsAll(int[] paramArrayOfInt)
  {
    for (int k : paramArrayOfInt) {
      if (getItem(new int[] { k }) == null)
        return false;
    }
    return true;
  }

  public boolean containsOneOf(int[] paramArrayOfInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    for (RSItem localRSItem : arrayOfRSItem1) {
      for (int n : paramArrayOfInt) {
        if (localRSItem.getID() == n)
          return true;
      }
    }
    return false;
  }

  public boolean isFull()
  {
    return getCount() == 28;
  }

  public boolean isItemSelected()
  {
    return getSelectedItemIndex() != -1;
  }

  public boolean useItem(RSItem paramRSItem1, RSItem paramRSItem2)
  {
    if (this.methods.game.getCurrentTab() != 4) {
      this.methods.game.openTab(4);
    }
    return (paramRSItem1.doAction("Use")) && (paramRSItem2.doAction("Use"));
  }

  public boolean useItem(RSItem paramRSItem, RSObject paramRSObject)
  {
    if (this.methods.game.getCurrentTab() != 4) {
      this.methods.game.openTab(4);
    }
    return (paramRSItem.doAction("Use")) && (paramRSObject.doAction("Use"));
  }

  public Point randomizeItemPoint(Point paramPoint)
  {
    return new Point(paramPoint.x + random(-10, 10), paramPoint.y + random(-10, 10));
  }

  public String getSelectedItemName()
  {
    String str = this.methods.client.getSelectedItemName();
    return (!isItemSelected()) || (str == null) ? null : str.replaceAll("<[\\w\\d]+=[\\w\\d]+>", "");
  }

  public int getSelectedItemIndex()
  {
    RSComponent[] arrayOfRSComponent = getInterface().getComponents();
    for (int i = 0; i < Math.min(28, arrayOfRSComponent.length); i++) {
      if (arrayOfRSComponent[i].getBorderThickness() == 2)
        return i;
    }
    return -1;
  }

  public RSItem getSelectedItem()
  {
    int i = getSelectedItemIndex();
    return i == -1 ? null : getItemAt(i);
  }

  public boolean clickSelectedItem(boolean paramBoolean)
  {
    RSItem localRSItem = getSelectedItem();
    return (localRSItem != null) && (localRSItem.doClick(true));
  }

  public boolean clickSelectedItem()
  {
    return clickSelectedItem(true);
  }

  public RSItem getItemAt(int paramInt)
  {
    RSComponent localRSComponent = getInterface().getComponent(paramInt);
    return (0 <= paramInt) && (paramInt < 28) && (localRSComponent != null) ? new RSItem(this.methods, localRSComponent) : null;
  }

  public RSItem[] getItems()
  {
    RSComponent localRSComponent1 = getInterface();
    if ((localRSComponent1 != null) && 
      (localRSComponent1.getComponents().length > 0)) {
      int i = 0;
      RSComponent[] arrayOfRSComponent = localRSComponent1.getComponents();
      for (Object localObject2 : arrayOfRSComponent) {
        if (localObject2.getType() == 5) {
          i++;
        }
      }
      ??? = new RSItem[i];
      for (??? = 0; ??? < i; ???++) {
        RSComponent localRSComponent2 = arrayOfRSComponent[???];
        int m = localRSComponent2.getComponentIndex();
        if (m >= 0)
          ???[m] = new RSItem(this.methods, localRSComponent2);
        else {
          return new RSItem[0];
        }
      }
      return ???;
    }

    return (RSItem)new RSItem[0];
  }

  public RSItem[] getItems(int[] paramArrayOfInt)
  {
    LinkedList localLinkedList = new LinkedList();
    for (RSItem localRSItem : getItems()) {
      for (int n : paramArrayOfInt) {
        if (localRSItem.getID() == n) {
          localLinkedList.add(localRSItem);
          break;
        }
      }
    }
    return (RSItem[])localLinkedList.toArray(new RSItem[localLinkedList.size()]);
  }

  public RSItem[] getCachedItems()
  {
    RSComponent localRSComponent1 = this.methods.interfaces.getComponent(149, 0);

    if ((localRSComponent1 != null) && 
      (localRSComponent1.getComponents().length > 0)) {
      int i = 0;
      for (Object localObject2 : localRSComponent1.getComponents()) {
        if (localObject2.getType() == 5) {
          i++;
        }
      }

      ??? = new RSItem[i];
      for (??? = 0; ??? < i; ???++) {
        RSComponent localRSComponent2 = localRSComponent1.getComponents()[???];
        int m = localRSComponent2.getComponentIndex();
        if (m >= 0)
          ???[m] = new RSItem(this.methods, localRSComponent2);
        else {
          return new RSItem[0];
        }
      }

      return ???;
    }

    return (RSItem)new RSItem[0];
  }

  public int getItemID(String paramString)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    int i = -1;
    for (RSItem localRSItem : arrayOfRSItem1) {
      RSItemDef localRSItemDef = localRSItem.getDefinition();
      if ((localRSItemDef != null) && (localRSItemDef.getName().contains(paramString))) {
        i = localRSItem.getID();
        break;
      }
    }
    return i;
  }

  public RSItem getItem(int[] paramArrayOfInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    for (RSItem localRSItem : arrayOfRSItem1) {
      for (int n : paramArrayOfInt) {
        if (localRSItem.getID() == n)
          return localRSItem;
      }
    }
    return null;
  }

  public int getCountExcept(int[] paramArrayOfInt)
  {
    return getCountExcept(false, paramArrayOfInt);
  }

  public int getCountExcept(boolean paramBoolean, int[] paramArrayOfInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    int i = 0;
    for (RSItem localRSItem : arrayOfRSItem1) {
      if (localRSItem.getID() == -1) {
        continue;
      }
      int m = 0;
      for (int i2 : paramArrayOfInt) {
        if (localRSItem.getID() == i2) {
          m = 1;
          break;
        }
      }
      if (m == 0) {
        i += (paramBoolean ? localRSItem.getStackSize() : 1);
      }
    }
    return i;
  }

  public int getCount(int[] paramArrayOfInt)
  {
    return getCount(false, paramArrayOfInt);
  }

  public int getCount(boolean paramBoolean, int[] paramArrayOfInt)
  {
    int i = 0;

    for (RSItem localRSItem : getItems()) {
      if (localRSItem == null)
      {
        continue;
      }
      for (int i1 : paramArrayOfInt) {
        if (localRSItem.getID() == i1) {
          i += (paramBoolean ? localRSItem.getStackSize() : 1);
        }
      }
    }

    return i;
  }

  public int getCount()
  {
    return getCount(false);
  }

  public int getCount(boolean paramBoolean)
  {
    int i = 0;
    RSItem[] arrayOfRSItem1 = getItems();
    for (RSItem localRSItem : arrayOfRSItem1) {
      int m = localRSItem.getID();
      if (m != -1) {
        if (paramBoolean)
          i += localRSItem.getStackSize();
        else {
          i++;
        }
      }
    }

    return i;
  }
}